import cv2 as cv
import argparse
from pathlib import Path
import utils


def main():
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True, help="path to the input image")
    ap.add_argument("-p", "--prefix", required=False, help="prefix for clipped image", default="converted")
    args = vars(ap.parse_args())

    image = cv.imread(args["image"])
    clipped_image = utils.detect(image)

    # parse file location
    image_path = Path(args["image"]).resolve()
    image_dir = image_path.parent
    image_name = image_path.name
    image_extension = image_path.suffix

    # save image
    cv.imwrite('{}/{}_{}{}'.format(image_dir, args["prefix"], image_name, image_extension), clipped_image)


if __name__ == "__main__":
    main()
